<template>
  <div class="page-Body">
    <h2 class="page-title">类型管理</h2>
    <!-- 表单 -->
    <el-form :inline="true" :model="formInline">
      <el-form-item label="分类名称">
        <el-input
          v-model="formInline.name"
          placeholder="请输入分类名称"
          clearable
          style="width: 250px"
          v-noSpace
        />
      </el-form-item>
      <el-form-item label="分类">
        <el-input
          v-model="formInline.parentName"
          placeholder="请输入标签分类"
          clearable
          style="width: 250px"
          v-noSpace
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="Search">搜索</el-button>
        <el-button @click="Reset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 新增对话框组件 -->
    <div class="el-DiaButton-block">
      <el-button
        plain
        @click="AddOpenDialog"
        class="el-DiaButton"
        type="primary"
      >
        添加
      </el-button>
    </div>

    <AddType @confirm="AddConfirm" @cancel="AddClose" :dialogAdd="dialogAdd" />
    <!-- 编辑对话框 -->
    <DetailsType
      @confirm="DetailsConfirm"
      @cancel="DetailsClose"
      :DetailsRow="DetailsRow"
      :dialogDetails="dialogDetails"
    />
    <!-- 表格 -->
    <el-table
      :data="tableData"
      border
      stripe
      fit
      empty-text="暂无数据"
      show-header
      v-loading="loading"
      size="default"
      element-loading-text="Loading..."
      :header-cell-style="{ background: '#e7e1fb' }"
      style="width: 100%"
      max-height="540px"
    >
      <el-table-column type="index" width="80" label="序号" />
      <el-table-column prop="name" label="分类名称" />
      <el-table-column prop="parentName" label="分类" />
      <el-table-column prop="createTime" label="添加时间" />
      <el-table-column prop="createByStr" label="添加者" />
      <el-table-column align="center" fixed="right" label="操作" v-slot="scope">
        <el-button link type="primary" @click="Edit(scope.row)">编辑</el-button>
        <el-button link type="danger" @click="Del(scope.row.id)">
          删除
        </el-button>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <div class="pagination-block">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[20, 40, 80, 100]"
        background
        layout="total,sizes, prev, pager, next,jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { delSupplyType, getSupplyTypeList } from '@/api/Activity/supply';
import { ref, onMounted } from 'vue';
import AddType from '@/components/Activity/supply/type/AddTypeSup.vue';
import DetailsType from '@/components/Activity/supply/type/DetailsTypeSup.vue';

// 分页变量
const currentPage = ref(1);
const pageSize = ref(20);
const total = ref(0);
// 对话框
const dialogAdd = ref(false);
const dialogDetails = ref(false);
const DetailsRow = ref({});
// 表单数据
const formInline = ref({
  name: '',
  parentName: '',
});
// 列表数据+表单校验
const tableData = ref([]);
const loading = ref(true);
// 分页逻辑
const handleSizeChange = async (val) => {
  pageSize.value = val;
  ExpertTypeList();
};
const handleCurrentChange = (val) => {
  currentPage.value = val;
  ExpertTypeList();
};
// 获取列表
const ExpertTypeList = async () => {
  loading.value = true;
  const res = await getSupplyTypeList({
    current: currentPage.value,
    size: pageSize.value,
    query: { ...formInline.value },
  });
  if (res.data.code === 200) {
    tableData.value = res.data.data.items || [];
    total.value = res.data.data.counts || 0;
    loading.value = false;
  }
};

// 搜索校验
const hasSearchConditions = () => {
  const { name, parentName } = formInline.value;
  return name || parentName;
};
// 搜索
const Search = async () => {
  if (!hasSearchConditions()) {
    // eslint-disable-next-line no-undef
    ElMessage({
      message: '请至少输入一个搜索条件',
      type: 'warning',
    });
    return;
  }
  ExpertTypeList();
};
// 重置
const Reset = () => {
  // 如果当前已经是空值状态，直接返回
  if (formInline.value.name === '' && formInline.value.parentName === '') {
    return;
  }
  formInline.value = {};
  ExpertTypeList();
};

// 删除
const Del = async (id) => {
  // eslint-disable-next-line no-undef
  ElMessageBox.confirm('确认删除该分类?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(async () => {
      const res = await delSupplyType(id);
      if (res.data.code === 200) {
        // eslint-disable-next-line no-undef
        ElMessage({
          message: '删除成功',
          type: 'success',
        });
        ExpertTypeList();
      } else {
        // eslint-disable-next-line no-undef
        ElMessage({
          message: res.data.msg,
          type: 'warning',
        });
      }
    })
    .catch(() => {});
};
// 编辑
const Edit = async (row) => {
  console.log(row);
  dialogDetails.value = true;
  DetailsRow.value = row;
};
const DetailsConfirm = () => {
  dialogDetails.value = false;
  ExpertTypeList();
};
const DetailsClose = () => {
  dialogDetails.value = false;
};
// 添加功能对话框
const AddOpenDialog = () => {
  dialogAdd.value = true;
};

const AddConfirm = () => {
  dialogAdd.value = false;
  ExpertTypeList();
};
const AddClose = () => {
  dialogAdd.value = false;
};
onMounted(() => {
  ExpertTypeList();
});
</script>

<style lang="scss" scoped>
.el-DiaButton-block {
  display: flex;
  .el-DiaButton {
    width: 120px;
    height: 40px;
    border-radius: 4px;
    margin-bottom: 20px;
    border: 1px solid #c7d4ff;
  }
}
</style>
